﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Jn698._45GWTool.Controls.DataControls;
using Jn698._45GWTool.Controls.RecordChoiceControls;
using Jn698._45GWTool.Models;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.Views;
using Jn698._45Tool.Service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using System.Windows.Controls;
using TouchMvvm;
using WebFirst.Entities;
using WindowsFormsApp1.Utils;

namespace Jn698._45GWTool.ViewModels
{
    public class RecordChoiceViewModel:ViewModelBase
    {
        private EditRecordListService editRecordListService = new EditRecordListService();
        public static EditRecordList EditRecord;

        private RecordChoiceModel _SelectedItem;
        /// <summary>
        /// 选择的项
        /// </summary>
        public RecordChoiceModel SelectedItem
        {
            get { return _SelectedItem; }
            set { _SelectedItem = value; RaisePropertyChanged(() => SelectedItem); }
        }

        private int selectedRSDIndex;

        public int SelectedRSDIndex
        {
            get { return selectedRSDIndex; }
            set {
                if (value<0)
                {
                    Set(ref selectedRSDIndex, 0);
                }
                else
                {
                    Set(ref selectedRSDIndex, value);
                }
                
            }
        }


        private ObservableCollection<RecordChoiceModel> _ListRecordChoice;
        /// <summary>
        /// 界面集合
        /// </summary>
        public ObservableCollection<RecordChoiceModel> ListRecordChoice
        {
            get { return _ListRecordChoice; }
            set { _ListRecordChoice = value; RaisePropertyChanged(() => ListRecordChoice); }
        }

        private string oad;

        public string OAD
        {
            get { return oad; }
            set { Set(ref oad, value); }
        }

        
        private DateTime dateTimeTrans;

        public DateTime DateTimeTrans
        {
            get { return dateTimeTrans; }
            set { Set(ref dateTimeTrans, value); }
        }

        public string RCSD { get; set; }
        public string RSD { get; set; }

        private ObservableCollection<RCSDDataModel> rcsdDataList;

        public ObservableCollection<RCSDDataModel> RcsdDataList
        {
            get { return rcsdDataList; }
            set { Set(ref rcsdDataList,value); }
        }


        private List<SingleData> roadList; 

        public List<SingleData> RoadList
        {
            get { return roadList; }
            set { roadList = value; }
        }

        


        /// <summary>
        /// 初始化函数
        /// </summary>
        private void MainInit()
        {
            if (ListRecordChoice == null)
            {

                ListRecordChoice = new ObservableCollection<RecordChoiceModel>() {
                new RecordChoiceModel()
                {
                    NameStr = "不选择",
                    ViewContent= null
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法1",
                    ViewContent = new RSD1Control(){ }
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法2",
                    ViewContent = new RSD2Control(){ }
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法3",
                    ViewContent = new RSD3Control(){ }
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法4",
                    ViewContent = new RSD4Control(){ }
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法5",
                    ViewContent = new RSD5Control(){}
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法6",
                    ViewContent = new RSD6Control(){ }
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法7",
                    ViewContent = new RSD7Control(){ }
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法8",
                    ViewContent = new RSD8Control(){ }
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法9",
                    ViewContent = new RSD9Control(){ }
                },
                new RecordChoiceModel()
                {
                    NameStr = "选择方法10",
                    ViewContent = new RSD10Control(){ }
                },
            };
            }

            DateTimeTrans = DateTime.Now;
            
            RcsdDataList = new ObservableCollection<RCSDDataModel>();
            RoadList = new List<SingleData>();
            RoadList.Add(new SingleData { Name = "对象属性描述符" });
            RoadList.Add(new SingleData { Name = "记录型对象属性描述符" });
            
            SelectedItem = ListRecordChoice[SelectedRSDIndex];

        }

        private void RCSDInit()
        {
            SelectedRSDIndex = Convert.ToSByte(EditRecord.StrRSD.Substring(0, 2),16);
            SelectedItem = ListRecordChoice[SelectedRSDIndex];
            int startPiont = 0;

            int csdNum = int.Parse(EditRecord.StrRCSD.Substring(startPiont, 2));
            startPiont += 2;
            for (int i = 0; i < csdNum; i++)
            {
                RCSDDataModel rCSDDataModel = new RCSDDataModel();
               
                if (int.Parse(EditRecord.StrRCSD.Substring(startPiont, 2)) == 0)
                {
                    rCSDDataModel.CSD = "对象属性描述符";
                    startPiont += 2;
                    if (startPiont + 8  > EditRecord.StrRCSD.Length)
                    {
                        break;
                    }
                    rCSDDataModel.Data = EditRecord.StrRCSD.Substring(startPiont, 8);
                    if (startPiont + 8 < EditRecord.StrRCSD.Length)
                    {
                        startPiont += 8;
                    }
                }
                else if (int.Parse(EditRecord.StrRCSD.Substring(startPiont, 2)) == 1)
                {
                    rCSDDataModel.CSD = "记录型对象属性描述符";
                    startPiont += 2;
                    if (startPiont + 8+2 > EditRecord.StrRCSD.Length)
                    {
                        break;
                    }
                    int oadNum = int.Parse(EditRecord.StrRCSD.Substring(startPiont + 8, 2));

                    rCSDDataModel.Data = EditRecord.StrRCSD.Substring(startPiont, 8 + 2 + oadNum * 8);
                    startPiont += 8 + 2 + oadNum * 8;
                }
                RcsdDataList.Add(rCSDDataModel);
            }
        }
        public RecordChoiceViewModel()
        {
            EditRecord = ServiceLocator.Current.GetInstance<EditRecordList>();
            MainInit();
            RCSDInit();
            OAD = EditRecord.StrOAD;
        }

        

        

        public RelayCommand DateTimeToClipCommand
        {
            get
            {
                var command = new RelayCommand(() => {
                    
                    Clipboard.Clear();//清空剪切板内容
                    
                    //Clipboard.SetData(DataFormats.Text, "复制内容");//复制内容到剪切板
                    Clipboard.SetDataObject("1C" + StringUtils.DateTime2String(dateTimeTrans));
                    
                });
                return command;
            }
        }

        public RelayCommand<Window> OKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) => {
                    w.DialogResult = true;
                    var rsdVM = ServiceLocator.Current.GetInstance<RSDViewModel>();
                    
                    RCSDDataModel rCSDDataModel;
                    
                    RCSD = RcsdDataList.Count.ToString("X2");
                    for (int i = 0; i < RcsdDataList.Count; i++)
                    {
                        rCSDDataModel = rcsdDataList[i];
                        if (rCSDDataModel.CSD == "对象属性描述符")
                        {
                            RCSD += "00";
                        }
                        else
                        {
                            RCSD += "01";
                        }
                        RCSD += rCSDDataModel.Data;
                    }
                    EditRecord.StrOAD = OAD;
                    EditRecord.StrRCSD = RCSD;
                    RSD = SelectedRSDIndex.ToString("X2");
                    switch (SelectedRSDIndex)
                    {
                        case 1:
                            RSD += rsdVM.RSD1OAD;
                            RSD += rsdVM.RSD1Data;
                            break;
                        case 2:
                            RSD += rsdVM.RSD2OAD;
                            RSD += rsdVM.DateTimeStartRSD2;
                            RSD += rsdVM.DateTimeEndRSD2;
                            RSD += rsdVM.DataRSD2;
                            break;
                        case 3:
                            RSD += rsdVM.RSD2DataList.Count.ToString("X2");
                            for(int i = 0;i < rsdVM.RSD2DataList.Count; i++)
                            {
                                RSD2Data rSD2Data = rsdVM.RSD2DataList[i];
                                RSD += rSD2Data.OAD;
                                RSD += rSD2Data.Start;
                                RSD += rSD2Data.End;
                                RSD += rSD2Data.TI;
                            }
                            break;
                        case 4:
                            RSD += StringUtils.DateTime2String(rsdVM.RSD4DateTimeStartP);
                            RSD += rsdVM.GetMSInfo();
                            break;
                        case 5:
                            RSD += StringUtils.DateTime2String(rsdVM.RSD5DateTimeStartP);
                            RSD += rsdVM.GetMSInfo();
                            break;
                        case 6:
                        case 7:
                        case 8:
                            RSD += StringUtils.DateTime2String(rsdVM.RSD6DateTimeStartP);
                            RSD += StringUtils.DateTime2String(rsdVM.RSD6DateTimeEndP);
                            RSD += rsdVM.SelectedRSD6TI.ToString("X2");
                            RSD += rsdVM.DataRSD6.ToString("X4");
                            RSD += rsdVM.GetMSInfo();
                            break;
                        case 9:
                            RSD += rsdVM.DataRSD9.ToString("X2");
                            break;
                        case 10:
                            RSD += rsdVM.DataRSD9.ToString("X2");
                            RSD += rsdVM.GetMSInfo();
                            break;
                        default:
                            break;
                    }
                    EditRecord.StrRSD = RSD;
                    editRecordListService.Update(EditRecord);
                    
                });
                return command;
            }
        }

        public RelayCommand<Window> CancelCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) => {
                    w.DialogResult = false;

                });
                return command;
            }
        }

        public RelayCommand<int> MouseDoubleClickCommand
        {
            get
            {
                var command = new RelayCommand<int>((index) =>
                {
                    if (index < 0|| index>= RcsdDataList.Count)
                    {
                        return;
                    }
                    RCSDDataModel rCSDData =RcsdDataList[index];
                    
                    if (rCSDData.CSD!=null&&rCSDData.CSD.Equals("记录型对象属性描述符"))
                    {
                        SimpleIoc.Default.Unregister<ROADViewModel>();
                        SimpleIoc.Default.Unregister<RCSDDataModel>();
                        SimpleIoc.Default.Unregister<RSDViewModel>();
                        GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register(() => RcsdDataList[index]);
                        GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register<ROADViewModel>();
                        GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register<RSDViewModel>();
                        ROADView rOADView = new ROADView();
                        if (rOADView.ShowDialog() == true)
                        {
                            /*gWCmdMakeUpPlanService.Update(ModeBitViewModel.GWCmdMakeUp);
                            SetDataList[index].DataValue = ModeBitViewModel.GWCmdMakeUp.DataValue;*/
                        }
                    }
                    
                });
                return command;
            }
        }

    }
}
